public class MyLinkedList implements IList{
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode Last;

    @Override
    public void disPlay() {
        ListNode cur=head;
        while (cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
    }

    @Override
    public int size() {
        int count=0;
        ListNode cur=head;
        while (cur!=null){
            count++;
        }
        return count;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur=head;
        while (cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    @Override
    public void addFirst(int date) {
        ListNode node=new ListNode(date);
        if(head==null){
            head=Last=node;
        }else{
            head.prev=node;
            node.next=head;
            head=node;
        }

    }

    @Override
    public void addLast(int date) {
        ListNode node=new ListNode(date);
        if(head==null){
            head=Last=node;
        }else{
            node.prev=Last;
            Last.next=node;
            Last=node;
        }

    }

    @Override
    public void addIndex(int index, int date) {
        int len=size();

        if(index<0||index>len){
            return;
        }
        if(index==0){
            addFirst(date);
            return;
        }
        if (index==len){
            addLast(date);
            return;
        }
        ListNode node=new ListNode(date);
        ListNode cur=findIndex(index);
        node.next=cur;
        cur.prev.next=node;
        node.prev=cur.prev;
        cur.prev=node;

    }

    private ListNode findIndex(int index){
        ListNode cur=head;
        while(index!=0){
            cur=cur.next;
            index--;
        }
        return cur;
    }

    @Override
    public void remove(int key) {
        ListNode cur=head;
        while(cur!=null){
            if (key==cur.val){
                if(head==cur){
                    head=head.next;
                    if(head!=null){
                        head.prev=null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if (cur.next == null) {
                        Last = Last.prev;
                    } else {
                        cur.next.prev = cur.prev;
                    }
                }
              return;
            }
            cur=cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur=head;
        while(cur!=null){
            if (key==cur.val){
                if(head==cur){
                    head=head.next;
                    if(head!=null){
                        head.prev=null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if (cur.next == null) {
                        Last = Last.prev;
                    } else {
                        cur.next.prev = cur.prev;
                    }
                }
            }
            cur=cur.next;
        }
    }

    @Override
    public void clear() {
        ListNode cur=head;
        while (cur!=null){
            ListNode curN=cur.next;
            cur.next=null;
            cur.prev=null;
            cur=curN;
        }
        head=Last=null;
    }
}
